#!/usr/bin/perl
#folderman-lib.pl

do "../web-lib.pl";
&init_config();
%access = get_module_acl();

sub get_mnt_root
{
	my $mntroot = "";
	open(STOR, $config{'storage_file'});
	while (<STOR>) {
		if (/^\s*volroot=\s*(\S*).*$/)
			{
				$mntroot = $1;
				break;
			}
	}
	close(STOR);
	return $mntroot;
}

sub list_folders
{
	my $dir = $_[0];
	my %tabfile;
	my (%htmltab, $linestr, $tmpline, $k);
	read_sharetab(\%tabfile, 2);
	if (!$dir)
	{
		print "<table width=100% class=mode2><tr>\n";
		print "<td valign=top><div class=main><table class=mode1 border=0 cellspacing=0 cellpadding=1 width=100%>\n";
		print "<tr class=title1> <th align=left>$text{'volumename'}</th> ",
			"<th align=left width=25%>$text{'date'}</th> ",
			"<th align=left width=15%>$text{'attributes'}</th> ",
			"</tr>\n";
		open(STOR, $config{'storage_file'});
		my $n = 0;
		my $mntroot;
		while (<STOR>) {
			if (/^\s*volroot=\s*(\S*).*$/)
			{
				$mntroot = $1;
				next;
			}
			elsif (/^\s*#/)
			{
				next;
			}
			elsif (/^\s*(\S+)\s+\S*\s+(\S*).*$/)
			{
				$pname = $1;
				$mntstatus = $2;
				next if ($mntstatus !~ /^normal$/);
			}
			else
			{
				next;
			}
			$path = "$mntroot$pname";
			if (!(-d $path)) { next; }
			$n++;
			if ($n % 2 eq 0)
			{
				$dismode = "class=mode2";

			}
			else
			{
				$dismode = "class=mode1";

			}
#			print "<tr $dismode>\n";
			$linestr = "<tr $dismode>\n";
			my $icon = "../images/dir.gif";
			my $link = "<a href=\"\" onclick='enter_dir(\"$path/\"); return false'>";
#			print "<td>", $link, "<img border=0 src=$icon>$pname</a></td> ";
			$linestr .= "<td>" . $link . "<img border=0 src=$icon>" . $pname . "</a></td> ";
			my @st = stat($path);
			my @tm = localtime($st[9]);
#			printf "<td nowarp $dismode>%.2d/%s/%.4d %.2d:%.2d</td>\n", $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900, $tm[2], $tm[1];
			$tmpline = sprintf "<td nowarp $dismode>%.2d/%s/%.4d %.2d:%.2d</td>\n", $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900, $tm[2], $tm[1];
			$linestr .= $tmpline;
			my $attr = '';
			$attr .='R ' if ($st[2] & 0004);
			$attr .='W ' if ($st[2] & 0002);
			$attr .='E ' if ($st[2] & 0001);
			$attr = $text{'none'} if (!$attr);

#			print "<td nowarp>", &html_escape($attr), "</td>\n";
			$tmpline = sprintf "<td nowarp>", &html_escape($attr), "</td>\n";
			$linestr .= $tmpline;
#			if (exists($tabfile{$path}))
#			{
#				my $stype;
#				$stype .='W ' if ($tabfile{$path}{'sharetype'} =~ /W/);
#				$stype .='U ' if ($tabfile{$path}{'sharetype'} =~ /U/);
#				$stype .='F ' if ($tabfile{$path}{'sharetype'} =~ /F/);
#				$stype .='H ' if ($tabfile{$path}{'sharetype'} =~ /H/);
#				print "<td nowarp $dismode>$stype</td>\n";
#			}
#			else
#			{
#				print "<td nowarp $dismode>$text{'none'}</td>\n";
#			}
#			print "</tr>\n";
			$linestr .= "</tr>\n";
			$htmltab{$pname} = $linestr;
		}
		close(STOR);

		foreach $k (sort keys %htmltab)
		{
			print $htmltab{$k};
		}

		print "</table></div></td>\n";
		print "<td valign=top><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
		if ($n)
		{
#			print "<tr><td align=center><input type=submit name=sharedir value=\"$text{'share_property'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n";
		}
		print "</table></td> </tr>\n";
		print "<tr><td colspan=2><i>$text{'attributes_hint'}</i></td></tr>\n";
		print "<tr><td colspan=2><i>$text{'sharetypes_hint'}</i></td></tr>\n";
		print "</table>\n";
		return $n;
	}

	$dir =~ /^(.*\/)[^\/]+\/$/;

	#print "<a href=index.cgi?dir=$1>", $text{'parentdir'}, "</a>\n";

	#print "<form name=listform>\n";
	print "<table width=100% class=mode2><tr>\n";
	print "<td valign=top><div class=main><table class=mode1 border=0 bordercolor=#000000 cellspacing=0 cellpadding=1 width=100%>\n";
	print "<tr><td><b>", &text('choose_dir', $dir), "</b></td><td colspan=3 align=right><a href=\"\" onclick='enter_dir(\"$1\"); return false'>$text{'parentdir'}</a></td></tr>\n";
	print "<tr class=title1> <th align=left>$text{'foldername'}</th> ",
		"<th align=left width=25%>$text{'date'}</th> ",
		"<th align=left width=15%>$text{'attributes'}</th> ",
		"<th align=left width=15%>$text{'sharetype'}</th> </tr>\n";


	opendir(DIR, $dir);
	my $n = 0;
	foreach $f (sort {$a cmp $b} readdir(DIR)){
		my $path = "$dir$f";
		if ($f eq "." || $f eq "..") { next; }
		if (!(-d $path)) { next; }
		if($f eq $config{'lost+found'})	{ next;	}
		$n++;
		#print "<tr $cb>\n";
		if ($n % 2 eq 0)
		{
			$dismode = "class=mode2";

		}
		else
		{
			$dismode = "class=mode1";

		}



		print "<tr $dismode>\n";
		my $icon = "../images/dir.gif";
		my $link = "<a href=\"\" onclick='enter_dir(\"$path/\"); return false'>";
		print "<td><input type=radio name=subdir value='$path'>$link<img border=0 src=$icon>$f</a></td> ";
		my @st = stat($path);
		my @tm = localtime($st[9]);
		printf "<td nowarp $dismode>%.2d/%s/%.4d %.2d:%.2d</td>\n", $tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900, $tm[2], $tm[1];
		my $attr = '';
		$attr .='R ' if ($st[2] & 0004);
		$attr .='W ' if ($st[2] & 0002);
		$attr .='E ' if ($st[2] & 0001);
		$attr = $text{'none'} if (!$attr);

		print "<td nowarp>", &html_escape($attr), "</td>\n";
		if (exists($tabfile{$path}))
		{
			my $stype;
			$stype .='W ' if ($tabfile{$path}{'sharetype'} =~ /W/);
			$stype .='U ' if ($tabfile{$path}{'sharetype'} =~ /U/);
			$stype .='F ' if ($tabfile{$path}{'sharetype'} =~ /F/);
			$stype .='H ' if ($tabfile{$path}{'sharetype'} =~ /H/);
			$stype .='A ' if ($tabfile{$path}{'sharetype'} =~ /A/);
			print "<td nowarp $dismode>$stype</td>\n";
		}
		else
		{
			print "<td nowarp $dismode>$text{'none'}</td>\n";
		}
		print "</tr>\n";
	}
	closedir(DIR);
	print "<tr><td colspan=4  align=right><a href=\"\" onclick='enter_dir(\"$1\"); return false'>$text{'parentdir'}</a></td></tr>\n";
	print "</table></div></td>\n";
	print "<td width=160 valign=top><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
	print "<tr><td align=center><input type=submit name=newdir value=\"$text{'create_folder'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n";
#	if ($n)
#	{
	printf "<tr><td align=center><input type=submit %s name=deldir value=\"$text{'delete_folder'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n", ($n)?"":"disabled=true";
	printf "<tr><td align=center><input type=submit %s name=editdir value=\"$text{'folder_property'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n", ($n)?"":"disabled=true";
	printf "<tr><td align=center><input type=submit %s name=sharedir value=\"$text{'share_property'}\" onclick='return SetEditName(this, $n)' class=button></td></tr>\n", ($n)?"":"disabled=true";
#	}
	print "</table></td> </tr>\n";
	print "<tr><td colspan=2><i>$text{'attributes_hint'}</i></td></tr>\n";
	print "<tr><td colspan=2><i>$text{'sharetypes_hint'}</i></td></tr>\n";
	print "</table>\n";

#	print "</form>\n";
	#print "<a href=index.cgi?dir=$1>", $text{'parentdir'}, "</a>\n";
	return $n;
}

#the 2nd parameter: 1--key is sharename; 2--key is sharepath
sub read_sharetab
{
	local $a = $_[0];
	local $k = $_[1];
	open(SHARETAB, $config{'sharetab_file'});
	while($line = <SHARETAB>)
	{
		$line =~ s/\r|\n//g;
		$line =~ s/#.*$//g;
		$line =~ s/\/\/.*$//g;

		if ($line)
		{
			while($line=~ /\\$/)
			{
				local $nl = <SHARETAB>;
				$line =~ s/\\$//;
				$nl =~ s/^\s+//;
				$line.=$nl;
			}
			local ($path, $sharename, $sharetype, $comment) = split(/:/, $line);
			$a->{$sharename} = {sharepath => $path, sharetype => $sharetype, comment => $comment} if ($k ==1);
			$a->{$path} = {sharename => $sharename, sharetype => $sharetype, comment => $comment} if ($k ==2);
		}

	}
	close(SHARETAB);
}

1;
